package com.beyondsoft.common.activiti.support;

import com.beyondsoft.common.activiti.exception.ActivitiException;
import com.beyondsoft.common.activiti.exception.ResultCode;
import com.beyondsoft.common.activiti.model.WorkflowTaskData;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangyan
 * @version $ Id: QueryTaskSupport.java, v 0.1 2020/6/20 11:47 zhangyan Exp $
 */
@Component
public class QueryTaskSupport {

    @Resource
    private ProcessEngine processEngine;

    public WorkflowTaskData queryTaskById(String taskId) {
        if (StringUtils.isBlank(taskId)) {
            throw new ActivitiException(ResultCode.ILLEGAL_PARAM.getCode(), "任务编号不能为空");
        }
        Task task = processEngine.getTaskService()
                .createTaskQuery()
                .taskId(taskId)
                .singleResult();
        return this.genWorkFlow(task);

    }

    public WorkflowTaskData queryTaskByProcInstId(String procInstId) {
        if (StringUtils.isBlank(procInstId)) {
            throw new ActivitiException(ResultCode.NO_DATA.getCode(), "流程实例编号不能为空");
        }
        Task task = processEngine.getTaskService()
                .createTaskQuery()
                .processInstanceId(procInstId)
                .orderByTaskCreateTime()
                .desc()
                .singleResult();
        return this.genWorkFlow(task);

    }

    public WorkflowTaskData queryTaskByBusinessKey(String businessKey) {
        if (StringUtils.isBlank(businessKey)) {
            throw new ActivitiException(ResultCode.NO_DATA.getCode(), "流程实例编号不能为空");
        }
        Task task = processEngine.getTaskService()
                .createTaskQuery()
                .processInstanceBusinessKey(businessKey)
                .orderByTaskCreateTime()
                .desc()
                .singleResult();
        return this.genWorkFlow(task);

    }

    private WorkflowTaskData genWorkFlow(Task task) {
        if (task == null) {
            throw new ActivitiException(ResultCode.NO_DATA.getCode(), "任务不存在");
        } else {
            WorkflowTaskData wfd = new WorkflowTaskData();
            wfd.setTaskId(task.getId());
            wfd.setTaskName(task.getName());
            wfd.setAssignee(task.getAssignee());
            wfd.setProcDefKey(task.getProcessDefinitionId());
            wfd.setProcInstId(task.getProcessInstanceId());
            wfd.setTaskDefKey(task.getTaskDefinitionKey());
            wfd.setCreateTime(task.getCreateTime());
            wfd.setExecutionId(task.getExecutionId());
            return wfd;
        }
    }

    public List<WorkflowTaskData> queryGroupTask(String groupId) {
        List<Task> list = processEngine.getTaskService()
                .createTaskQuery()
                .taskCandidateUser(groupId)
                .orderByTaskCreateTime().asc()
                .list();
        List<WorkflowTaskData> dataList = new ArrayList<>();
        for (Task task : list) {
            WorkflowTaskData workflowTaskData = genWorkFlow(task);
            dataList.add(workflowTaskData);
        }
        return dataList;
    }
}